package org.yogurtcat.server.modules.transaction.service;

import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.yogurtcat.server.modules.transaction.dao.TransactionRepository;
import org.yogurtcat.server.modules.transaction.dao.TransactionSpec;
import org.yogurtcat.server.modules.transaction.domain.Transaction;
import org.yogurtcat.server.modules.transaction.domain.TransactionVo;

/**
 * 交易业务类
 * @author heaven
 *
 */
@Service
public class TransactionServiceImpl implements TransactionService{
	
	@Autowired
	private TransactionRepository transactionRepository;
	
	@Override
	public Page<Transaction> list(Optional<TransactionVo> condition, Pageable pageable) {
		Page<Transaction> transactions = transactionRepository.findAll(TransactionSpec.builder().condition(condition).build(), pageable);
		return transactions;
	}

	@Override
	public Transaction save(TransactionVo data) {
		Transaction target = new Transaction();
		BeanUtils.copyProperties(data, target);
		transactionRepository.save(target);
		return target;
	}

	@Override
	public void delete(TransactionVo data) {
		Transaction target = new Transaction();
		BeanUtils.copyProperties(data, target);
		transactionRepository.delete(target);
	}
}
